package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
public class NursingProjectServiceImpl  implements NursingProjectService {
     @Autowired
     private NursingProjectMapper nursingProjectMapper;
    @Override
    public PageResponse selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page1= nursingProjectMapper.selectByPage(name, status);
        return PageResponse.of(page1, NursingProjectVo.class);
    }

    @Override
    public int save(NursingProjectDto project) {
        //重名校验
        if (nursingProjectMapper.getByName(project.getName()) != null) {
            throw new BaseException(BasicEnum. NURSING_INSERT_FAIL_CODE);
        }
        NursingProject nursingProject = BeanUtil.copyProperties(project, NursingProject.class);
        return nursingProjectMapper.save(nursingProject);
    }

    @Override
    public int updateById(NursingProjectDto project) {
        if (nursingProjectMapper.getPlanById(project.getId())>0){
            throw new BaseException(BasicEnum.NURSINUSE_INSERT_FAIL_CODE);
        }
        //重名校验
        if (nursingProjectMapper.getByName1(project.getName(),project.getId()) != null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_NAME_INSERT_FAIL_CODE);
        }
        NursingProject nursingProject = BeanUtil.copyProperties(project, NursingProject.class);
        return nursingProjectMapper.updateById(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject project= nursingProjectMapper.getById(id);
        NursingProjectVo nursingProjectVo = BeanUtil.copyProperties(project, NursingProjectVo.class);
        return nursingProjectVo;
    }

    @Override
    public int removeById(Long id) {
        if (nursingProjectMapper.getPlanById(id)>0){
            throw new BaseException(BasicEnum.NURSINUSE_INSERT_FAIL_CODE);
        }
        return nursingProjectMapper.removeById(id);
    }

    @Override
    public List<NursingProjectVo> getAll() {
       List<NursingProject> projects= nursingProjectMapper.getAll();
       List<NursingProjectVo> projectVos = new ArrayList<>();
       projects.forEach(project -> {
           NursingProjectVo nursingProjectVo = BeanUtil.copyProperties(project, NursingProjectVo.class);
           projectVos.add(nursingProjectVo);
       });


        return projectVos;
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        if (nursingProjectMapper.getPlanById(id)>0){
            throw new BaseException(BasicEnum.NURSINUSE_INSERT_FAIL_CODE);
        }
        return nursingProjectMapper.updateStatus(id, status);
    }
}
